﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.User;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domian.Entitys;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.User
{
    public class GetCityHandler : IRequestHandler<GetCityCommand, ApiResult<List<CityDto>>>
    {
        private readonly IBaseRepository<CityModel> _cityRepository;
        private readonly IMapper _mapper;

        public GetCityHandler(IBaseRepository<CityModel> cityRepository, IMapper mapper)
        {
            _cityRepository = cityRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 查询城市
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<CityDto>>> Handle(GetCityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<CityDto>>result = new ApiResult<List<CityDto>>();

            var city = _cityRepository.GetAll().Where(c => c.ProvinceId == request.ProvinceId).ToList();
            var model =_mapper.Map<List<CityDto>>(city);
            result.Code = StatusEnum.OK;
            result.Data = model;
            result.Message = "获取列表成功";
            return Task.FromResult(result);
        }
    }
}
